/******************************************************************************\
*  Animeshell 1.0 Multimedia manager                                           *
*  Copyright (C) 2006-2007 Rémy Guillemette <shaikadzari@gmail.com>            *
*                                                                              *
*  This program is free software; you can redistribute it and/or modify        *
*  it under the terms of the GNU General Public License version 2 only as      *
*  published by the Free Software Foundation.                                  *
*                                                                              *
*  This program is distributed in the hope that it will be useful,             *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
*  GNU General Public License for more details.                                *
*                                                                              *
*  You should have received a copy of the GNU General Public License           *
*  along with this program; if not, write to the                               *
*  Free Software Foundation, Inc.,                                             *
*  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                   *
\******************************************************************************/
#include <QSqlQuery>

#include "mysqliteclass.h"


//


mysqliteclass::mysqliteclass( QString pDriver, QString pDatabase, QString pUser, QString pPasswd ) 
: sqlclassbase( pDriver, pDatabase, pUser, pPasswd)
{}


QList<animeclass> mysqliteclass::GetLineFromTable()
{
	animeclass a;
	QList<animeclass> table;
	
	
	if ( LookForTable( "animedb" ) == -1 )
	{
		return table;
	}	
		
		
	QSqlQuery q( "select noseq, name, noind, typedata, genredata1, genredata2,"
				 " typesupport, quotation, description, reference, imagefile "
				 " from animedb", db ); 
	
	q.setForwardOnly( true );
	
	while ( q.next() )
	{
		a.setNoSeq( q.value(0).toInt() );
		a.setName( q.value(1).toString() );
		a.setIndice( q.value(2).toInt() );
		a.setTypeData( q.value(3).toInt() );
		a.setGenreData1( q.value(4).toInt() );
		a.setGenreData2( q.value(5).toInt() );
		a.setTypeSupport( q.value(6).toInt() );
		a.setQuotation( q.value(7).toInt() );
		a.setDescription( q.value(8).toString() );
		a.setReference( q.value(9).toString() );
		a.setImageFile( q.value(10).toString() );
		
		table.append(a);
		
		//qDebug("%d", a.getQuotation());
	}
	
	return table;
}


int mysqliteclass::size()
{
	return LookForTable( "animedb" );
}

//
animeclass mysqliteclass::GetLineFromTable(int pNoSeq)
{
	animeclass a;
	QSqlQuery q("select noseq, name, noind, typedata, genredata1, genredata2, "
			    " typesupport, quotation, description, reference, imagefile "
			    " from animedb where noseq = " + QString( pNoSeq ), db );
	
	q.setForwardOnly( true );	    
	
	while ( q.next() )
	{
		a.setNoSeq( q.value(0).toInt() );
		a.setName( q.value(1).toString() );
		a.setIndice( q.value(2).toInt() );
		a.setTypeData( q.value(3).toInt() );
		a.setGenreData1( q.value(4).toInt() );
		a.setGenreData2( q.value(5).toInt() );
		a.setTypeSupport( q.value(6).toInt() );
		a.setQuotation( q.value(7).toInt() );
		a.setDescription( q.value(8).toString() );
		a.setReference( q.value(9).toString() );
		a.setImageFile( q.value(10).toString() );
	}
	
	return a;
}

animeclass mysqliteclass::GetLastInsert()
{
	animeclass a;
	QSqlQuery q("SELECT  noseq, name, noind, typedata, genredata1, genredata2, "
			    " typesupport, quotation, description, reference, imagefile "
			    " FROM animedb ORDER BY noseq DESC", db );
	if( q.next() )
	{
		a.setNoSeq( q.value(0).toInt() );
		a.setName( q.value(1).toString() );
		a.setIndice( q.value(2).toInt() );
		a.setTypeData( q.value(3).toInt() );
		a.setGenreData1( q.value(4).toInt() );
		a.setGenreData2( q.value(5).toInt() );
		a.setTypeSupport( q.value(6).toInt() );
		a.setQuotation( q.value(7).toInt() );
		a.setDescription( q.value(8).toString() );
		a.setReference( q.value(9).toString() );
		a.setImageFile( q.value(10).toString() );
	}
	
	return a;
	
}

bool mysqliteclass::SetNewLineInTable(animeclass pNewLine)
{
    QSqlQuery q;
    q.prepare("INSERT INTO animedb "
    		  "VALUES( NULL, :NAME, :IND, :TYPD, :GER1, :GER2, :TYPS, :QUO, :DES, :REF, :IMG)");
    q.bindValue( ":NAME", pNewLine.getName() );
    q.bindValue( ":IND", pNewLine.getIndice() );
    q.bindValue( ":TYPD", pNewLine.getTypeData() );
    q.bindValue( ":GER1", pNewLine.getGenreData1() );
    q.bindValue( ":GER2", pNewLine.getGenreData2() );
    q.bindValue( ":TYPS", pNewLine.getTypeSupport() );
    q.bindValue( ":QUO", pNewLine.getQuotation() );
    q.bindValue( ":DES", pNewLine.getDescription() );
    q.bindValue( ":REF", pNewLine.getReference() );
    q.bindValue( ":IMG", pNewLine.getImageFile() );
    
    
    if ( q.exec() )
    	return true;
    
    return false;
}


bool mysqliteclass::UpdateTable(animeclass pLine)
{
	QSqlQuery q;
    q.prepare("UPDATE animedb SET"
    		  " name = :NAME,"
    		  " noind = :IND,"
    		  " typedata = :TYPD,"
    		  " genredata1 = :GER1,"
    		  " genredata2 = :GER2,"
			  " typesupport = :TYPS,"
			  " quotation = :QUO,"
			  " description = :DES,"
			  " reference = :REF,"
			  " imagefile = :IMG"
			  " WHERE noseq = :SEQ");
			  
    q.bindValue( ":NAME", pLine.getName() );
    q.bindValue( ":IND", pLine.getIndice() );
    q.bindValue( ":TYPD", pLine.getTypeData() );
    q.bindValue( ":GER1", pLine.getGenreData1() );
    q.bindValue( ":GER2", pLine.getGenreData2() );
    q.bindValue( ":TYPS", pLine.getTypeSupport() );
    q.bindValue( ":QUO", pLine.getQuotation() );
    q.bindValue( ":DES", pLine.getDescription() );
    q.bindValue( ":REF", pLine.getReference() );
    q.bindValue( ":IMG", pLine.getImageFile() );
    q.bindValue( ":SEQ", pLine.getNoSeq() );
    
    if ( q.exec() )
    	return true;
    	
    return false;
}


bool mysqliteclass::DeleteLineFromTable(int pNoSeq)
{
	// Prepare the qsqlquery
	QSqlQuery q;
    q.prepare("DELETE FROM animedb"
			  " WHERE noseq = :SEQ");
	
	// We need a key!
	if ( pNoSeq > 0 )
	{
		// Set the primary key to use
		q.bindValue("SEQ", QString::number( pNoSeq ));
		
		// Execute the query
		if ( q.exec() )
    		return true;
	}
	
	return false;		  
}
//
